const find=require('./index.js')

let getCaseList=function(page,size){
    let _sql=`SELECT caselist.id,caselist.title,casetype.typeName FROM caselist,casetype WHERE caselist.parentId = casetype.typeId Order By id Desc LIMIT ${(page-1)*size},${size}`
    return find.query(_sql)
}
let getCaseListTotal = function(){
    let _sql = 'SELECT COUNT(*) FROM caselist'
    return find.query(_sql)
}
let getCase=function(id){
    let _sql=`SELECT caselist.id,caselist.title,caselist.parentId,caselist.imgUrl,caselist.caseDetial FROM caselist,casetype WHERE caselist.id=${id} AND caselist.parentId = casetype.typeId`
    return find.query(_sql)
}
let upDateCase = function(title,parentId,imgUrl,caseDetial,id){
    let _sql = `UPDATE caselist SET title = '${title}', parentId = '${parentId}', imgUrl = '${imgUrl}', caseDetial = '${caseDetial}' WHERE id = '${id}'`
    return find.query(_sql)
}
let addCase = function(title,parentId,imgUrl,caseDetial){
    let _sql = `INSERT INTO caselist (title,parentId,imgUrl,caseDetial) VALUES('${title}','${parentId}','${imgUrl}','${caseDetial}')`
    return find.query(_sql)
}
let postArticle = function(title,typeName,time, isLove,description,imgUrl,html){
    let _sql = `INSERT INTO article (title,parentId,time, isLove,description,imgUrl,html) VALUES ('${title}','${typeName}','${time}','${isLove}','${description}','${imgUrl}','${html}')`
    return find.query(_sql)
}

let delateArticle = function(id){
    let _sql = `DELETE FROM article WHERE id="${id}";`
    return find.query(_sql)
}

let login = function(username, password){
    let _sql = `SELECT * FROM cos_org_user WHERE C_NAME="${username}" AND C_PASSWORD="${password}";`
    return find.query(_sql)
}
let getTypeInfo = function(){
    let _sql = `SELECT typeName FROM typeinfo`
    return find.query(_sql)
}
let upDateArticle = function(title,typeName,time, isLove,description,imgUrl,html,id){
    let _sql = `UPDATE article SET title = '${title}', parentId = '${typeName}', time = '${time}', isLove = '${isLove}', description = '${description}', imgUrl = '${imgUrl}', html = \"${html}\" WHERE id = '${id}'`
    return find.query(_sql)
}
let insertToSql=function(title){
    let _sql = `INSERT INTO test (catalog) VALUES ('${title}')`
    return find.query(_sql)
}
let insertToSql1=function(val,catalog,imgUrl,pid,time){
    let _sql = `INSERT INTO caselist (title,caseDetial,imgUrl,parentId,time) VALUES ('${catalog}','${val}','${imgUrl}','${pid}','${time}')`
    return find.query(_sql)
}

module.exports={getCaseList,getCaseListTotal,postArticle, delateArticle, login, getTypeInfo,upDateArticle,getCase,upDateCase,addCase,insertToSql,insertToSql1}